﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using VirtualCamUI.Infrastructure;
using VirtualCamUI.Services;

namespace VirtualCamUI.ViewModels
{
    internal class MainWindowViewModel : ViewModelBase
    {
        #region memebers
        private int _delay = 1;
        private ImageSource _image;
        private Timer _imageupdate;
        private IImageSourceService _model;

        private RelayCommand _setpath;
        private RelayCommand _delayup;
        private RelayCommand _delaydown;
        private RelayCommand _startprocess;
        private RelayCommand _stopprocess;
        #endregion

        #region Properties
        public int Delay
        {
            set { _delay = value;
            _imageupdate.Interval = _delay * 1000;
            OnPropertyChanged("Delay");
            }
            get { return _delay; }
        }

        public string Path
        {
            set { _model.SourceUri = value;
            OnPropertyChanged("Path");
            }
            get { return _model.SourceUri; }
        }

        public ImageSource Image
        {
            set { _image = value;
            OnPropertyChanged("Image");
            }
            get { return _image; }
        }
        #endregion

        #region Commands
        public RelayCommand SetPath
        {
            get { return _setpath ?? (_setpath = new RelayCommand(SetSourcePath)); }
        }

        public RelayCommand DelayUp
        {
            get { return _delayup ?? (_delayup = new RelayCommand(() => { if(Delay <9) Delay++; })); }
        }

        public RelayCommand DelayDown
        {
            get { return _delaydown ?? (_delaydown = new RelayCommand(() => { if(Delay > 1) Delay--; })); }
        }

        public RelayCommand StartProcessing
        {
            get { return _startprocess ?? (_startprocess = new RelayCommand(StartProcessingImages)); }
        }

        public RelayCommand StopProcessing
        {
            get { return _stopprocess ?? (_stopprocess = new RelayCommand(StopProcessingImages)); }
        }
        #endregion

        #region Constructors
        internal MainWindowViewModel(IImageSourceService source)
        {
            _model = source;
            _imageupdate = new Timer();
            _imageupdate.Tick += (s, e) => { Image = _model.GetNextImage(); };
            _imageupdate.Interval = Delay * 1000;
        }

        internal MainWindowViewModel() : this(new ImageFileService()) { }
        #endregion

        private void SetSourcePath()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if(fbd.ShowDialog() == DialogResult.OK)
            {
                Path = fbd.SelectedPath;
            }
        }

        private void StartProcessingImages()
        {
            _imageupdate.Enabled = true;
            _imageupdate.Start();
        }

        private void StopProcessingImages()
        {
            _imageupdate.Stop();
            _imageupdate.Enabled = false;
        }
    }
}
